<template>
  <div class="employees">
    <el-card class="top">
      <el-button
        v-control="'employees_import_button'"
        type="primary"
        size="mini"
        @click="toImport"
      >导入</el-button>
      <el-button type="primary" size="mini" @click="add">+ 新增员工</el-button>
    </el-card>
    <el-card>
      <el-table :data="employees" border>
        <el-table-column label="序号" width="100px">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.size + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="username" width="120px" />
        <el-table-column label="头像" width="100px">
          <template v-slot="{ row }">
            <div style="text-align: center">
              <img
                v-imgerror="imgerror"
                :src="row.staffPhoto"
                class="img"
                alt=""
                @click="showQrCode(row.staffPhoto)"
              >
            </div>
          </template>
        </el-table-column>
        <el-table-column label="手机号" prop="mobile" width="150px" />
        <el-table-column label="工号" prop="workNumber" width="120px" />
        <el-table-column
          label="聘用形式"
          prop="formOfEmployment"
          width="120px"
          :formatter="formatterEmployment"
        />
        <el-table-column label="部门" prop="departmentName" width="150px" />
        <el-table-column label="入职时间" width="150px">
          <template v-slot="{ row }">
            <div>
              {{ row.timeOfEntry | formatTime }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="是否在职"
          prop="enableState"
          width="120px"
          :formatter="formatterWorkingState"
        />
        <el-table-column label="状态" width="120px">
          <template v-slot="{ row }">
            <div>
              {{ row.enableState === 1 ? '可用' : '不可用' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="300px" fixed="right">
          <template v-slot="{ row }">
            <div class="btn">
              <el-button type="text" @click="toDetails(row.id)">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <el-button
                type="text"
                @click="toAssignRole(row.id)"
              >角色</el-button>
              <el-button type="text" @click="del(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          :current-page="page.page"
          :page-sizes="[1, 5, 10, 20]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @current-change="pageChange"
          @size-change="sizeChange"
        />
      </div>
    </el-card>
    <DialogItem ref="dialogItem" @pageChange="pageChange" />
    <el-dialog :visible.sync="isShow" title="二维码" width="40%">
      <div style="text-align: center">
        <canvas ref="canvas" />
      </div>
    </el-dialog>
    <!-- 分配角色弹框 -->
    <AssignRole ref="assignRole" />
  </div>
</template>

<script>
import { sysUser, sysUserDel } from '@/api/employees.js'
import DialogItem from './components/dialog-item.vue'
import employees from '@/assets/constant/employees.js'
import QrCode from 'qrcode'
import AssignRole from './components/assign-role.vue'
export default {
  name: 'Employees',
  components: {
    DialogItem,
    AssignRole
  },
  data() {
    return {
      employees: [],
      page: {
        page: 1,
        size: 10,
        total: 0
      },
      imgerror: require('@/assets/common/bigUserHeader.png'),
      isShow: false
    }
  },
  created() {
    this.getEmployees()
  },
  methods: {
    // 获取员工数据
    async getEmployees() {
      const res = await sysUser(this.page)
      this.employees = res.data.rows
      this.page.total = res.data.total
    },
    // 分页导航改变页码
    pageChange(page) {
      this.page.page = page
      this.getEmployees()
    },
    // 分页导航改变页容量
    sizeChange(size) {
      this.page.size = size
      this.page.page = 1
      this.getEmployees()
    },
    // 新增员工
    add() {
      this.$refs.dialogItem.isShow = true
    },
    // 格式化聘用形式
    formatterEmployment(row, column, cellValue, index) {
      const res = employees.hireType.find((item) => {
        return item.id == cellValue
      })
      return res ? res.value : '-'
    },
    // 格式化是否在职
    formatterWorkingState(row, column, cellValue, index) {
      const res = employees.workingState.find((item) => {
        return item.id == cellValue
      })
      return res ? res.value : '-'
    },
    // 删除员工
    del(id) {
      this.$confirm('您确定要删除该员工吗？', '温馨提示').then(async() => {
        await sysUserDel(id)
        this.$message.success('删除员工成功')
        this.pageChange(1)
      })
    },
    // 导入点击
    toImport() {
      this.$router.push('/employees/import?name=' + encodeURIComponent('员工'))
    },
    // 查看点击
    toDetails(id) {
      this.$router.push('/employees/details/' + id)
    },
    // 点击头像展示二维码
    showQrCode(imgUrl) {
      if (!imgUrl) {
        return this.$message.error('暂无头像')
      }
      this.isShow = true
      this.$nextTick(() => {
        QrCode.toCanvas(this.$refs.canvas, imgUrl, {
          width: 300,
          height: 300
        })
      })
    },
    // 角色点击
    toAssignRole(id) {
      this.$refs.assignRole.isShow = true
      this.$refs.assignRole.getUserInfo(id)
    }
  }
}
</script>

<style lang="scss" scoped>
.employees {
  padding: 20px;
  .top {
    text-align: right;
    margin-bottom: 15px;
  }
  ::v-deep thead > tr > th:nth-child(11) {
    text-align: center;
  }
  ::v-deep tbody > tr > td {
    padding: 10px 0;
  }
  .btn {
    text-align: center;
  }
  .pagination {
    text-align: center;
    margin-top: 20px;
  }
  .img {
    width: 40px;
    height: 40px;
    object-fit: contain;
    cursor: pointer;
  }
}
</style>
